<template>
  <div class="element-library-search-input">
    <input class="_input"
           @keydown.enter="searchEvent"
           v-model="value"
           @input="valueInput"
           :placeholder="placeholder"/>
    <i class="fa search-button tw-px-2" @click="clickEvent"
       :class="{'fa-search': !searchMode, 'fa-close': searchMode}"/>
  </div>
</template>

<script>
  export default {
    name: "ElementLibrarySearchInput",
    model: {
      prop: 'value',
      event: "input"
    },
    props: {
      value: {
        type: String,
        default: ""
      },
      permitEmpty: {
        type: Boolean,
        default: false
      },
      searchMode: {
        default: false,
        type: Boolean
      },
      placeholder: {
        default: "",
        type: String
      }
    },
    methods: {
      valueInput() {
        this.$emit("input", this.value);
      },
      searchEvent() {
        if (this.value.trim() === "" && !this.permitEmpty) return;
        this.$emit("search");
      },
      clickEvent() {
        if (this.value.trim() === "" && !this.permitEmpty) return;
        this.$emit("click");
      }
    }
  }
</script>

<style scoped>
  .element-library-search-input {
    background-color: var(--bg-color1);
    border: 1px solid var(--border-color3);
    margin-left: 0.5rem;
    margin-right: 0.5rem;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    align-items: center;
    overflow: hidden;
    border-radius: 0.25rem;
  }

  .element-library-search-input:hover {
    border: 1px solid var(--border-color5);
  }

  .search-button {
    cursor: pointer;
  }

  ._input {
    -webkit-box-flex: 1;
    background-color: transparent;
    border-style: none;
    color: var(--text-color1);
    -webkit-flex: 1 1 0%;
    flex: 1 1 0%;
    outline: 2px solid transparent;
    outline-offset: 2px;
    padding: .25rem .5rem !important;
  }
</style>